package com.aiju.cash.widgit;

import android.content.Context;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.base.dialog.BaseDialog;
import com.aiju.bean.Discount;
import com.aiju.bean.DiscountType;
import com.aiju.cash.R;
import com.aiju.common.PixValue;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.Util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-12-29 11:44
 * @version: V1.0
 * @Description: 描述
 */
public class DiscountDialog extends BaseDialog {

    private LinearLayout linearLayout, linearLayout2;
    private List<Discount> discounts = new ArrayList<>();
    private View card;

    public DiscountDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.LodingDialogStyle;
    }

    @Override
    protected View getView() {
        View inflate = mLayoutInflater.inflate(R.layout.dialog_discount, null);
        linearLayout = (LinearLayout) inflate.findViewById(R.id.ly_discount);
        linearLayout2 = (LinearLayout) inflate.findViewById(R.id.ly_discount_bottom);
        card = inflate.findViewById(R.id.set_card);
        card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mOnClickDiscount != null) {
                    mOnClickDiscount.onCard(v);
                }
            }
        });
        return inflate;
    }

    public DiscountDialog setDiscountList(List<DiscountType> list) {
        discounts.clear();
        for (DiscountType type : list) {
            discounts.addAll(type.getList());
        }
        if (Util.isEmpty(discounts)) {
            return this;
        }

        Collections.sort(discounts, new Comparator<Discount>() {
            @Override
            public int compare(Discount o1, Discount o2) {
                if (o1.getComparator() > o2.getComparator()) {
                    return 1;  //正数
                } else if (o1.getComparator() < o2.getComparator()) {
                    return -1;  //负数
                } else {
                    return 0;  //相等为0
                }
            }
        });
        for (Discount discount : discounts) {
            buidDiscountView(discount);
        }
        return this;
    }

    /**
     * 构建优惠视图
     *
     * @param discount
     */
    private void buidDiscountView(final Discount discount) {
        final TextView view = new TextView(mContext);
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        view.setGravity(Gravity.CENTER);
        view.setTextColor(Color.WHITE);
        view.setPadding(PixValue.dp.valueOf(25), 0, PixValue.dp.valueOf(25), 0);
        TextPaint tp = view.getPaint();
        tp.setFakeBoldText(true);

        LinearLayout.LayoutParams params;
        if (discount.getType() == 1) {
            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, PixValue.dp.valueOf(50));
            view.setMinWidth(PixValue.dp.valueOf(60));
            view.setText(PriceUtil.formatDiscount(discount.getValue()) + "折");
            view.setBackgroundResource(R.mipmap.icon_discount_zk_on);
            linearLayout.setVisibility(View.VISIBLE);
            linearLayout.addView(view, params);
        } else if (discount.getType() == 2) {
            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, PixValue.dp.valueOf(50));
            view.setMinWidth(PixValue.dp.valueOf(120));
            view.setText("减" + PriceUtil.formatDouble(discount.getValue()) + "元");
            view.setBackgroundResource(R.mipmap.icon_discount_jj_on);
            linearLayout.setVisibility(View.VISIBLE);
            linearLayout.addView(view, params);
        } else if (discount.getType() == 3) {
            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setMinWidth(PixValue.dp.valueOf(160));
            view.setPadding(PixValue.dp.valueOf(25), PixValue.dp.valueOf(20), PixValue.dp.valueOf(25), PixValue.dp.valueOf(20));
            view.setText("满" + PriceUtil.formatDouble(discount.getCondition()) + "元\n减" + PriceUtil.formatDouble(discount.getValue()) + "元");
            view.setBackgroundResource(R.mipmap.icon_discount_jj_on);
            linearLayout2.setVisibility(View.VISIBLE);
            linearLayout2.addView(view, params);
        } else {
            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setMinWidth(PixValue.dp.valueOf(160));
            view.setPadding(PixValue.dp.valueOf(25), PixValue.dp.valueOf(20), PixValue.dp.valueOf(25), PixValue.dp.valueOf(20));
            view.setText("满" + PriceUtil.formatDouble(discount.getCondition()) + "元\n打" + PriceUtil.formatDiscount(discount.getValue()) + "折");
            view.setBackgroundResource(R.mipmap.icon_discount_zk_on);
            linearLayout2.setVisibility(View.VISIBLE);
            linearLayout2.addView(view, params);
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickDiscount != null) {
                    mOnClickDiscount.onDiscount(discount, view.getText().toString());
                }
                dismiss();
            }
        });

    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public DiscountDialog setLayoutParams() {
        getDialogWindow().setWindowAnimations(R.style.share_animation);
        getDialogWindow().setGravity(Gravity.BOTTOM);
        getDialogWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
        return this;
    }

    public DiscountDialog setOnClickDiscount(OnClickDiscount mOnClickDiscount) {
        this.mOnClickDiscount = mOnClickDiscount;
        return this;
    }

    private OnClickDiscount mOnClickDiscount;

    public interface OnClickDiscount {
        void onDiscount(Discount discount, String msg);

        void onCard(View view);
    }

}
